<% layout('/layouts/default.html', {title: 'st_take_stock_bill管理', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(stTakeStockBill.isNewRecord ? '新增st_take_stock_bill' : '编辑st_take_stock_bill')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btn-bill" title="${text('生成单据')}"><i class="fa fa-filter"></i> ${text('生成单据')}</a>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>

			</div>
			<script id="importTpl" type="text/template">//<!--
			生成盘亏: <input id="gen-less" type="checkbox" class="form-control" checked="checked" onclick="return false"/>
			&nbsp;&nbsp;&nbsp;生成盘盈: <input id="gen-more" type="checkbox" class="form-control" checked="checked" onclick="return false"/>
//--></script>
			<script>
				$("#btn-bill").click(function () {
					js.layer.open({
						type: 1,
						area: ['400px'],
						title: '${text("导入用户数据")}',
						resize: false,
						scrollbar: true,
						content: js.template('importTpl'),
						success: function(layero, index){
							layero.find('input[type="checkbox"]').iCheck();
						},
						btn: ['<i class="fa fa-check"></i> ${text("导入")}',
							'<i class="fa fa-remove"></i> ${text("关闭")}'],
						btn1: function(index, layero){
							var less = layero.find('#gen-less').is(":checked");
							var more = layero.find('#gen-more').is(":checked");
							$.ajax({
								url:"${ctx}/st/stInventoryBill/take",
								dataType:'json',
								type:'POST',
								data:{
									//请求参数
									...genMap(),
									less,
									more,
								},
								success:function(result){
									js.layer.closeAll();
								}
							});
							// var form = {
							// 	inputForm: layero.find('#inputForm'),
							// 	file: layero.find('#file').val()
							// };
							// if (form.file == '' || (!js.endWith(form.file, '.xls') && !js.endWith(form.file, '.xlsx'))){
							// 	js.showMessage("${text('文件不正确，请选择后缀为“xls”或“xlsx”的文件。')}", null, 'warning');
							// 	return false;
							// }
							// js.ajaxSubmitForm(form.inputForm, function(data){
							// 	js.showMessage(data.message);
							// 	if(data.result == Global.TRUE){
							// 		js.layer.closeAll();
							// 	}
							// 	page();
							// }, "json");
							return false;
						}
					});
				});
				function genMap(){
					var head = "stInventoryBillDetailList";
					var nameMap={
						"materialCode": "materialName",
						"unit": "unit",
						"batchNumber" :"batchNumber",
						"warehouseId" :"warehousePosition",
						"diffNumber" :"number",
						"productDate": "productDate",
						"savedType" :"savedType",
						"passDate": "passDate",
					};
					var $tableTr = $("table").find("tr");
					var map = {};
					for (var i = 2; i < $tableTr.size(); i++) {
						var tr = $tableTr[i];
						var $trTd = $(tr).find("td");
						for (var j = 0; j < $trTd.size(); j++) {
							var td = $trTd[j];
							var $input = $(td).find("input");
							map[head + "[" + (i - 2) + "]." + nameMap[$input.attr("name")]] = $input.val();
						}
					}
					map["primaryId"] = $("#id").val();
					return map;
				}
			</script>
		</div>
		<#form:form id="inputForm" model="${stTakeStockBill}" action="${ctx}/st/stTakeStockBill/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<div class="row">
<!--					<div class="col-xs-6">-->
<!--						<div class="form-group">-->
<!--							<label class="control-label col-sm-4" title="">-->
<!--								<span class="required hide">*</span> ${text('bill_no')}：<i class="fa icon-question hide"></i></label>-->
<!--							<div class="col-sm-8">-->
<!--								<#form:input path="billNo" maxlength="20" class="form-control"/>-->
<!--							</div>-->
<!--						</div>-->
<!--					</div>-->
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('盘点方案名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billName" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('bill_date')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="billDate" readonly="true" maxlength="20" class="form-control laydate"
									dataFormat="datetime" data-type="datetime" data-format="yyyy-MM-dd HH:mm"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('take_man_code')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="takeManCode" maxlength="20" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('take_man')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="takeMan" maxlength="5" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('锁定物料')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="lockStatus" maxlength="3" class="form-control digits"/>
							</div>
						</div>
					</div>
				</div>
				<h4 class="form-unit">${text('盘点详情')}</h4>
				<div class="ml10 mr10">
					<table id="stTakeStockDetailDataGrid"></table>
					<% if (hasPermi('st:stTakeStockBill:edit')){ %>
						<a href="#" id="stTakeStockDetailDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('st:stTakeStockBill:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/bignumber.js/9.0.1/bignumber.js"></script>
<% } %>
<script>
//初始化盘点详情DataGrid对象
$("#stTakeStockDetailDataGrid").dataGrid({

	data: ${toJson(stTakeStockBill.stTakeStockDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("物料编号")}', name:'materialCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control',readonly: true}},
		{header:'${text("物料名称")}', name:'materialName', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'30', 'class':'form-control',readonly: true}},
		{header:'${text("单位")}', name:'unit', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control',readonly: true}},
		{header:'${text("批号")}', name:'batchNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'30', 'class':'form-control',readonly: true}},
		{header:'${text("仓位")}', name:'warehouseName', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control',readonly: true}},
		{header:'${text("仓位")}', name:'warehouseId', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'36', 'class':'form-control',readonly: true},hidden: true},
		{header:'${text("库存数量")}', name:'warehouseNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control digits',readonly: true}},
		{header:'${text("实际数量")}', name:'realNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control',dataInit: function(element){

					$(element).bind("input propertychange", function () {
						var v2 = this.value;
						if(!this.value) v2 = 0;
						var $closest = $(this).closest("td");
						var v1 = $closest.prev().find(".form-control").val();
						var n1 = new BigNumber(v1);
						var n2 = new BigNumber(v2);
						$closest.next().find(".form-control").val(n2.minus(n1).toNumber());
					});
				}
			}
		},
		{header:'${text("差异数量")}', name:'diffNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'20', 'class':'form-control',readonly: true},formatter: function(val, obj, row, act){
				var n1 = new BigNumber(row.warehouseNumber);
				var n2 = new BigNumber(row.realNumber || 0);
				return n2.minus(n1).toNumber();
		}},
		{header:'${text("生产日期")}', name:'productDate', width:150,
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					// laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("保质期")}', name:'savedType', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control digits',readonly: true}},
		{header:'${text("过期日期")}', name:'passDate', width:150,
			formatter:'date', formatoptions:{srcformat:'Y-m-d H:i:s',newformat:'Y-m-d H:i:s'},
			editable:true, edittype:'text', editoptions:{'class':'form-control laydate', 'readonly':'true',
				dataInit: function(element){
					// laydate.render({elem:element, type:'datetime', format:'yyyy-MM-dd HH:mm'});
				}
			}
		},
		{header:'${text("parent_id")}', name:'parentId.id', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stTakeStockDetailDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#stTakeStockDetailDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],

	// 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#stTakeStockDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'stTakeStockDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,id,materialCode,materialName,unit,batchNumber,warehouseName,warehouseNumber,realNumber,productDate,savedType,passDate,parentId.id,diffNumber,warehouseId', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
</script>